package com.kedsoft.andaily.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kedsoft.andaily.common.CommonList;
import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Cunsumer;
import com.kedsoft.andaily.entity.CunsumerLogin;
import com.kedsoft.andaily.service.CunsumerLoginService;
import com.kedsoft.andaily.service.CunsumerService;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-10
 */
@Controller
@RequestMapping("/cunsumer")
public class CunsumerController extends BaseController {

    @Autowired
    CunsumerService cunsumerService;

    @Autowired
    CunsumerLoginService cunsumerLoginService;

    @RequestMapping("index")
    public String index(){
        return "cunsumer/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Cunsumer> getPageList(Pagination pagination, @RequestParam(required = false) String name,
                                        @RequestParam(required = false) String mobile,
                                        @RequestParam(required = false) String routername){
        EntityWrapper<Cunsumer> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("name",name);
        entityWrapper.like("mobile",mobile);
        entityWrapper.like("routername",routername);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Cunsumer> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        cunsumerService.selectPage(page,entityWrapper);
        PageVo<Cunsumer> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "cunsumer/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Cunsumer param){
        cunsumerService.insert(param);
        CunsumerLogin cunsumerLogin = new CunsumerLogin();
        EntityWrapper<Cunsumer> wrapper = new EntityWrapper<>();
        wrapper.eq("mobile",param.getMobile());
        Cunsumer bean = cunsumerService.selectOne(wrapper);
        cunsumerLogin.setCunsumerid(bean.getId());
        cunsumerLogin.setCunsumername(bean.getMobile());
        cunsumerLogin.setCunsumerpwd("123456");
        cunsumerLogin.setLeader(bean.getLeader());
        cunsumerLoginService.insert(cunsumerLogin);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Cunsumer param){
        cunsumerService.updateById(param);
        //先删除登录信息
        EntityWrapper<CunsumerLogin> wrapper = new EntityWrapper<>();
        wrapper.eq("cunsumerid",param.getId());
        cunsumerLoginService.delete(wrapper);
        //添加登录信息
        CunsumerLogin cunsumerLogin = new CunsumerLogin();
        cunsumerLogin.setCunsumerid(param.getId());
        cunsumerLogin.setCunsumername(param.getMobile());
        cunsumerLogin.setCunsumerpwd("123456");
        cunsumerLogin.setLeader(param.getLeader());
        cunsumerLoginService.insert(cunsumerLogin);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "initpwd")
    @ResponseBody
    public MessageResult initpwd(@RequestParam String mobile){
        CunsumerLogin cunsumerLogin = new CunsumerLogin();
        cunsumerLogin.setCunsumerpwd("123456");
        EntityWrapper<CunsumerLogin> wrapper = new EntityWrapper<>();
        wrapper.eq("cunsumername",mobile);
        cunsumerLoginService.update(cunsumerLogin,wrapper);
        return MessageResult.getSuccess("密码重置成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        cunsumerService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            cunsumerService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "cunsumer/search";
    }

    @RequestMapping(value = "routerCunsumer")
    @ResponseBody
    public CommonList<Cunsumer> findCunsumerByRouterId(@RequestParam Integer routerid){
        EntityWrapper<Cunsumer> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("routerid",routerid);
        entityWrapper.orderBy("id",false);
        List<Cunsumer> list = cunsumerService.selectList(entityWrapper);
        return CommonList.success(list);
    }

    @RequestMapping(value = "routerCunsumerLeader")
    @ResponseBody
    public CommonList<Cunsumer> findCunsumerLeaderByRouterId(@RequestParam Integer routerid){
        EntityWrapper<Cunsumer> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("routerid",routerid);
        entityWrapper.eq("leader",1);
        entityWrapper.orderBy("id",false);
        List<Cunsumer> list = cunsumerService.selectList(entityWrapper);
        return CommonList.success(list);
    }
}

